var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon: '../../images/logo.png',
    oldPassword: '',
    newPassword: '',
    confirmPassword: '',
    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  oldInput (e) {
    this.setData({
      oldPassword: e.detail.value
    })
  },

  newInput (e) {
    this.setData({
      newPassword: e.detail.value
    })
  },

  againInput (e) {
    this.setData({
      confirmPassword: e.detail.value
    })
  },

  updatePassword () {
    const oldPassword = this.data.oldPassword
    const newPassword = this.data.newPassword
    const confirmPassword = this.data.confirmPassword
    const that = this
    if (!oldPassword) {
      wx.showToast({
        title: '请输入原密码',
        icon: 'error',
        duration: 2000
      })
      return
    }
    if (!newPassword) {
      wx.showToast({
        title: '请输入新密码',
        icon: 'error',
        duration: 2000
      })
      return
    }
    if (!confirmPassword) {
      wx.showToast({
        title: '请确认新密码',
        icon: 'error',
        duration: 2000
      })
      return
    }
    if (confirmPassword!=newPassword) {
      wx.showToast({
        title: '密码确认错误',
        icon: 'error',
        duration: 2000
      })
      return
    }
    const dlzh = wx.getStorageSync('dlzh')
    wx.showLoading({
      title: '修改中',
    })
    this.setData({
      loading: true
    })
    app.wxpost({
      url:'wechart/updatePassword.html',
      data: {
        dlzh: dlzh,
        oldPassword: oldPassword,
        newPassword: newPassword
      },
      success: function (res) {
        that.setData({
          loading: false
        })
        wx.hideLoading()
        if (res.data.result) {
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000
          })
          wx.switchTab({
            url: '../../pages/home/home'
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'error',
            duration: 2000
          })
        }
      }
    })
  }
})